"""
Case Type   : gs_checkse工具功能
Case Name   : 验证i参数
Create At   : 2024/10/21
Owner       : lihongji
Description :
    1.gs_checkse检测A1-A10的安全项配置
    2.gs_checkse只检测A1的安全项配置
    3.gs_checkse修复所有项安全配置参数
    4.gs_checkse修复连接配置安全项参数
    5.gs_checkse检测A1、A2两项安全配置
    6.gs_checkse修复连接配置、文件目录安全项
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):

        step = '----step1:gs_checkse检测A1-A10的安全项配置 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertIn("A10.[ Other configurations ]",
                      check_res, '执行失败' + step)

        step = '----step2:gs_checkse只检测A1的安全项配置 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A1 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertNotIn("A10.[ Other configurations ]",
                      check_res, '执行失败' + step)

        step = '----step3:gs_checkse修复所有项安全配置参数 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("B1. [ Set Connection configuration ]", check_res,
                      '执行失败' + step)
        self.assertIn("B10.[ Set Other configurations ]", check_res,
                      '执行失败' + step)

        step = '----step4:gs_checkse修复连接配置安全项参数 expect:成功----'
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B1')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("B1. [ Set Connection configuration ]",
                      check_res, '执行失败' + step)
        self.assertNotIn("B10.[ Set Other configurations ]",
                      check_res, '执行失败' + step)

        step = ('----step5:gs_checkse检测A1、A2两项安全配置  expect:成功----')
        self.log.info(step)
        gs_checkse_cmd_1 = (f'source {self.env_path};'
                            f'gs_checkse -i A1 -i A2')
        self.log.info(gs_checkse_cmd_1)
        check_res_1 = self.common.get_sh_result(self.primaryNode,
                                                gs_checkse_cmd_1)
        gs_checkse_cmd_2 = (f'source {self.env_path};'
                            f'gs_checkse -i A1,A2')
        self.log.info(gs_checkse_cmd_2)
        check_res_2 = self.common.get_sh_result(self.primaryNode,
                                                gs_checkse_cmd_2)
        self.assertIn("A1. [ Checking Connection configuration ]",
                      check_res_1, '执行失败' + step)
        self.assertNotIn("A10.[ Other configurations ]",
                         check_res_1, '执行失败' + step)
        self.assertIn("A1. [ Checking Connection configuration ]",
                         check_res_2, '执行失败' + step)
        self.assertNotIn("A10.[ Other configurations ]",
                         check_res_2, '执行失败' + step)

        step = ('----step6:修复连接配置、文件目录安全项  expect:成功----')
        self.log.info(step)
        gs_checkse_cmd_1 = (f'source {self.env_path};'
                            f'gs_checkse -i B1 -i B2')
        self.log.info(gs_checkse_cmd_1)
        check_res_1 = self.common.get_sh_result(self.primaryNode,
                                                gs_checkse_cmd_1)
        gs_checkse_cmd_2 = (f'source {self.env_path};'
                            f'gs_checkse -i B1,B2')
        self.log.info(gs_checkse_cmd_2)
        check_res_2 = self.common.get_sh_result(self.primaryNode,
                                                gs_checkse_cmd_2)
        self.assertIn("B1. [ Set Connection configuration ]",
                      check_res_1, '执行失败' + step)
        self.assertNotIn("B10.[ Set Other configurations ]",
                         check_res_1, '执行失败' + step)
        self.assertIn("B1. [ Set Connection configuration ]",
                      check_res_2, '执行失败' + step)
        self.assertNotIn("B10.[ Set Other configurations ]",
                         check_res_2, '执行失败' + step)

    def tearDown(self):
        step = '----step5:无需清理环境 expect:成功----'
        self.log.info(step)
        self.log.info(f'----{os.path.basename(__file__)} end----')